package com.group.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.entity.Talents;
import com.group.mapper.TalentsMapper;
import com.group.service.TalentsService;
import com.group.vo.DataVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 朋心合力
 * @since 2021-08-09
 */
@Controller
@CrossOrigin
//@RequestMapping("/talents")
public class TalentsController {

    @Autowired
    TalentsService talentsService;

    @Autowired
    TalentsMapper talentsMapper;


    @RequestMapping("/lookingTalentsByTag")
    @ResponseBody
    public static String lookingTalentsByTag(@RequestParam("talents") String talents) {
        return talents;
    }


    //Postman 测试分页成功
    @GetMapping("/talentslist")
    public String talentsListRedirect(HttpSession session, Model model) {
        List<Talents> talentsList = (List<Talents>) session.getAttribute("talentsList");
        Long allPages = (Long) session.getAttribute("allPages");
        model.addAttribute("talentsList", talentsList);
        model.addAttribute("allPages", allPages);
        return "lookingtalents";
    }


    //通过人员Id查找人才
    @GetMapping("/queryTalentById")
    @ResponseBody
    public Talents queryTalentById(@RequestParam Integer userId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("userId", userId);
        Talents talents = talentsMapper.selectOne(wrapper);
        return talents;
    }


    @GetMapping("/getTalentList")
    @ResponseBody
    public DataVO getTalentList(Integer page, Integer limit,@RequestParam String talentTag) {
//        return talentsService.findData(page, limit);
        return talentsService.findData(page, limit,talentTag);
    }

    @GetMapping("/getTalentList2")
    @ResponseBody
    public DataVO getTalentList(Integer page, Integer limit) {
        return talentsService.findData2(page, limit);
    }

    @PostMapping("/modifyMyself")
    public String modifyMyself(@Nullable @RequestParam("talentAge") Integer talentAge,
                               @Nullable @RequestParam("talentIntro") String talentIntro,
                               @Nullable @RequestParam("talentTag") String[] talentTag,HttpSession session) {
        Integer currentUserId = (Integer) session.getAttribute("currentUserId");
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("userId", currentUserId);
        Talents talents = new Talents();
        if (talentAge != null) {
        talents.setTalentAge(talentAge);
        }
        if (talentIntro != null && !talentIntro.equals("")) {
        talents.setTalentIntro(talentIntro);
        }
        if (talentTag != null) {
        String talentTags = StringUtils.join(talentTag, ",");
        talents.setTalentTag(talentTags);
        }
        talentsService.update(talents, wrapper);
        return "redirect:/userInformation";
    }


}

